package com.briup.day41;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ChatClient2 {
	public static void main(String[] args) {
		String ip = "localhost";
		int port = 8888;
		try {
			// 创建客户端套接字对象
			// 注意：这里的ip实际上定义的是服务器的IP地址
			Socket socket = new Socket(ip, port);
			System.out.println("连接服务器成功！");
			
			// 向服务器发送一条信息
			OutputStream os = socket.getOutputStream();
			Scanner sc = new Scanner(System.in);
			String msg = sc.nextLine();
			
			os.write(msg.getBytes());
			os.flush();
			socket.shutdownOutput();
			
			// 接收服务器回复的信息
			InputStream is = socket.getInputStream();
			byte[] buf = new byte[128];
			int len = -1;
			while ((len = is.read(buf)) != -1) {
				System.out.print(new String(buf, 0, len));
			}
			socket.shutdownInput();
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
